package com.blueocean_health.care.management.mapper;

import com.blueocean_health.care.management.domain.base.OrderInfo;
import com.blueocean_health.care.management.em.OrderFlagEnum;
import com.blueocean_health.care.management.em.OrderStatusEnum;
import com.blueocean_health.care.management.util.MyMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface OrderInfoMapper extends MyMapper<OrderInfo> {

    @Select("SELECT * FROM order_info WHERE order_id =#{orderId} and deflag = 0 ORDER BY version DESC LIMIT 1")
    public OrderInfo getLastOrderInfoById(@Param("orderId") String orderId);

    @Select("SELECT * FROM order_info a WHERE VERSION = (SELECT MAX(VERSION) FROM order_info WHERE a.order_id = order_id) AND deflag = 0")
    public List<OrderInfo> getLastOrderInfoList();

    @Select("SELECT * FROM order_info a WHERE VERSION = (SELECT MAX(VERSION) FROM order_info WHERE a.order_id = order_id) AND deflag = 0 AND a.`order_id` =#{orderId}")
    public List<OrderInfo> getLastOrderInfoByOrderId(@Param("orderId") String orderId);

    @Update("UPDATE order_info SET order_update_flag = 1 ,order_status =#{orderStatus} ,update_time = NOW() WHERE id =#{id}")
    public Boolean updateOrderStatusById(@Param("orderStatus")String orderStatus,@Param("id")Integer id);

    @Update("UPDATE order_info SET account_flag =#{accountFlag} ,update_time = NOW() ,account_time =#{accountTime} WHERE id =#{id}")
    public Boolean updateOrderAccountFlagById(@Param("accountFlag")String accountFlag,@Param("id")Integer id,@Param("accountTime")String accountTime);

    @Update("UPDATE order_info SET deflag =#{deflag} ,update_time = NOW() WHERE order_id =#{orderId}")
    public Boolean updateOrderFlagByOrderId(@Param("deflag")String orderFlag, @Param("orderId")String orderId);

    @Update("UPDATE order_info SET renew_flag =#{renewFlag} ,update_time = NOW() WHERE order_id =#{orderId}")
    public Boolean updateRenewFlagByOrderId(@Param("renewFlag")String renewFlag, @Param("orderId")String orderId);

    @Update("UPDATE order_info SET pay_status =#{payStatus} ,update_time = NOW() WHERE id =#{id}")
    public Boolean updatePayStatusById(@Param("payStatus")String payStatus,@Param("id")Integer id);

    @Select("select * FROM order_info o where o.`refund_order_id`  =#{orderId} and o.`order_status` != 6 and o.`deflag` = 0 and o.`order_type` = 3")
    public List<OrderInfo> getRefundOrderBySuperOrderId(@Param("orderId")String orderId);

    @Update("UPDATE order_info SET deflag =#{deflag} ,update_time = NOW() WHERE refund_order_id =#{orderId}")
    public Boolean deleteOrderByRefundOrderId(@Param("deflag")String orderFlag, @Param("orderId")String orderId);
    /**
     * 修改已读
     * @param OrderUpdateFlag
     * @param orderId
     * @return
     */
    @Update("UPDATE order_info SET order_update_flag =#{OrderUpdateFlag} ,update_time = NOW() WHERE order_id =#{orderId}")
    public Boolean updateOrderUpdateFlag(@Param("OrderUpdateFlag")String OrderUpdateFlag, @Param("orderId")String orderId);
      /**
       * 查询数量
       */
    @Select("SELECT count(id) FROM order_info a WHERE VERSION = (SELECT MAX(VERSION) FROM order_info WHERE a.order_id = order_id) AND deflag = 0 and order_update_flag = 0 and hospital_id = #{hospitalId}" )
    public int selectUnreadOrder(@Param("hospitalId")String hospitalId);


    /**
    * @Description: 删除老版本号的数据
    * @param:入参  orderid  version
    * @return:出参
    * @throws:
    */
    @Delete("DELETE FROM order_info WHERE order_id =#{orderId} AND VERSION !=#{version}")
    int deleteOldVersionOrderByOrderId(@Param("orderId")String orderId,@Param("version") Long version );

    /**
     * 取消续费
     * 
     */
    @Update("UPDATE order_info SET renew_flag ='1' ,update_time = NOW() WHERE order_id =#{orderId}")
    public Boolean cancelrenewal(@Param("orderId")String orderId);
    /**
     * 修改付款入账状态
     * 
     */
    @Update("UPDATE order_info SET order_status=#{orderStatus},account_flag =#{accountFlag},pay_status=#{payStatus},update_time = NOW() WHERE order_id =#{orderId}")
    public Boolean changeOnlineOrderStatusStatus(@Param("orderId")String orderId,@Param("orderStatus")String orderStatus,@Param("accountFlag")String accountFlag,@Param("payStatus")String payStatus);

}